package com.atguigu.flink.tableapi;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.table.api.Expressions.$;

/**
 * Created by Smexy on 2023/2/5
 *
 */
public class Demo2_TableToStream
{
    public static void main(String[] args) {
        
       StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
       StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

       SingleOutputStreamOperator<WaterSensor> ds = env
             .socketTextStream("hadoop103", 8888)
             .map(new WaterSensorMapFunction());
       Table table = tableEnvironment.fromDataStream(ds);

        //打印表的结构 取决于流中的类型的属性
        table.printSchema();


        Table result = table.select($("id"), $("ts"), $("vc"))
                           .where($("id").isEqual("s1"));

        //把表再转换为流，调用StreamAPI操作
        DataStream<WaterSensor> ds2 = tableEnvironment.toDataStream(result, WaterSensor.class);

        ds2.print();


        try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
        
    }
}
